<?php if(!defined('IN_WDPHP'))exit('No direct script access.');
/**
 * WDPHP  			分页类
 * @package 		WDPHP
 * @author 			肖伟 
 * @version 		v1.0
 * @copyright 		Copyright (c) 2012. widuu.com
 */


	class Page {
		private $total; // 总条数
		private $page; 	// 当前页码
		private $count; // 总页数
		private $num; 	// 每页条数
		private $type; 	// 页码类型
		private $prev; 	// 上一页码
		private $next; 	// 下一页码
		private $offset;// 偏移
		private $flag; 	// 分页标记
		public  $diyStyle=false;//是否使用自定义CSS
		private $urlPattern; 	// 分页链接模板
		public $select=true;//是否显示跳转下拉框
		public $config=array('prev'=>'上一页','next'=>'下一页','first'=>'首页','last'=>'末页','theme'=>' <span>共%total%条记录</span><span>%nowPage%/%totalPage% 页</span>   %first% %prePage%  %linkPage%  %nextPage% %end%');
		
		/**
		 * @param number $total 数据条数
		 * @param number $num	每页条数,默认20
		 * @param string $urlPattern	分页URL,默认自动获取,用%page%代替页码
		 * @param string $flag	分页参数,默认page
		 */
		public function __construct($total, $num = 20,$flag = 'page',$urlPattern=null) {
			$this->total = $total;
			$this->num = $num;
			$this->flag=$flag;
			$this->urlPattern=$urlPattern;
			$this->page = empty ( $_GET[$flag] ) ? 1 : ( int ) $_GET[$flag];
			$this->count = $this->getCount ();
			$this->prev = $this->getPrev ();
			$this->next = $this->getNext ();
			$this->offset = $this->getOffset ();
		}
		
		// 计算页数
		private function getCount() {
			return ceil ( $this->total / $this->num );
		}
		
		// 计算前一页码
		private function getPrev() {
			if ($this->page <= 1)
				return false;
			else
				return $this->page - 1;
		}
		
		// 计算后一页码
		private function getNext() {
			if ($this->page >= $this->count)
				return false;
			else
				return $this->page + 1;
		}
		
		// 获取Limit
		public function getOffset() {
			return (($this->page - 1) * $this->num).','.$this->num;
		}
		
		// 获取分页Limit
		public function __get($proName) {
			if ($proName == 'limit')
				return $this->getOffset();
			if($proName=='pageNvi')
				return $this->getPageNvi();
			if($proName=='nowPage')
				return $this->page;
			return false;
		}
		
		//获取URL
		private function url($num) {
			$_GET[$this->flag]=$num;
			if(is_null($this->urlPattern)){// 如果没有传入模板
				$string=http_build_query($_GET);
				$url=$_SERVER['SCRIPT_NAME'].str_replace(array('m=','&a=','&','='),'/',$string);
			}else{// 如果有传入URL模板
				$url=str_replace('%page%', $num, $this->urlPattern);
			}
			return $url;
		}
		
		/**
		 * 构造分页代码
		 * @return string
		 */
		public function getPageNvi() {		
			if($this->count>1){//页数大于1
				$theFirst='<a href="'.$this->url(1).'" title="首页">'.$this->config['first'].'</a>';
				if($this->page>1)
					$prePage='<a href="'.$this->url($this->prev).'" title="上一页">'.$this->config['prev'].'</a>';
				else 
					$prePage='';
			}else{
				$theFirst='';
				$prePage='';
			}
			
			if($this->count>1){
				if($this->page<$this->count)
					$nextPage='<a href="'.$this->url($this->next).'" title="下一页">'.$this->config['next'].'</a>';
				else
					$nextPage='';
				$theEnd='<a href="'.$this->url($this->count).'" title="末页">'.$this->config['last'].'</a>';
			}else{
					$theEnd='';
					$nextPage='';
			}
				
			//1 2 3 4 5 6 7...
			$linkPage='';
			if($this->count<=7){
				for($i=1;$i<=$this->count;$i++){
					$class=($this->page==$i)?' class="current" ':'';
					$linkPage.='<a href="'.$this->url($i).'" '.$class.' title="第'.$i.'页">'.$i."</a>\n";
				}
			}elseif($this->page<5){
				for($i=1;$i<=7;$i++){
					$class=($this->page==$i)?' class="current" ':'';
					$linkPage.='<a href="'.$this->url($i).'" '.$class.' title="第'.$i.'页">'.$i."</a>\n";
				}
			}else{
				for($i=$this->page-3;$i<$this->page;$i++){
					$linkPage.='<a href="'.$this->url($i).'" title="第'.$i.'页">'.$i."</a>\n";
				}
				$linkPage.='<a href="'.$this->url($this->page).'" title="第'.$this->page.'页" class="current">'.$this->page."</a>\n";
				for($i=$this->page+1;$i<=$this->page+3;$i++){
					$linkPage.='<a href="'.$this->url($i).'" title="第'.$i.'页">'.$i."</a>\n";
				}
				$linkPage.='<span>...</span>';
			}
			$pageStr=str_replace(
				array('%total%','%nowPage%','%totalPage%','%first%','%prePage%','%linkPage%','%nextPage%','%end%'),
				array($this->total,$this->page,$this->count,$theFirst,$prePage,$linkPage,$nextPage,$theEnd),$this->config['theme']);
				if($this->diyStyle)//如果要自定义CSS
					$style='';
				else 
					$style='<style>.pageNvi{margin:10px auto; display:inline-block;}.pageNvi a:link,.pageNvi a:visited,.pageNvi span{color:#222;padding:3px 6px;text-decoration:none;border:solid 1px #DDDDDD;margin-right:1px; font-size:12px;}.pageNvi a:hover{border:solid 1px #666666;}.pageNvi a.current{color:#444;font-weight:bold;padding:6px 10px;}</style>';
				return $style.'<div class="pageNvi">'.$pageStr.'</div>';
			
		}
	}
	
